package mis.ds.ps.model.dao.impl;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import mis.ds.ps.model.dao.VersoesDao;
import mis.ds.ps.model.obj.Versoes;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.felees.hbnpojogen.persistence.impl.GenericHibernateDaoImpl;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class VersoesDaoImpl extends GenericHibernateDaoImpl<Versoes, Long> implements VersoesDao {

	public static final int ID = 1;
	public static final int NOME = 2;
	public static final int DATA = 3;
	public static final int DESCRICAO = 4;

	private static Logger logger = Logger.getLogger(DisciplinasDaoImpl.class);

	private Connection mysqlConn;

	/** Constructor method. */
	public VersoesDaoImpl() {
		super(Versoes.class);

		PropertyConfigurator.configure("log4j.properties");
	}

	public List<Versoes> getVersoes() {

		List<Versoes> versoesList = new ArrayList<Versoes>();

		String sql = "SELECT * from versoes";
		PreparedStatement prest;
		try {

			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
			logger.debug("SQL select statement [" + prest.toString() + "]");
			ResultSet rs = prest.executeQuery();
			while (rs.next()) {
				Versoes vs = new Versoes(Long.valueOf(rs.getString(ID)));
				vs.setNome(rs.getString(NOME));
				vs.setData(rs.getDate(DATA));
				vs.setDescricao(rs.getString(DESCRICAO));
				versoesList.add(vs);
			}

		} catch (SQLException e1) {
			logger.debug("Cannot insert versoes on DB ", e1);
		}
		return versoesList;

	}

	public boolean insertVersoes(Versoes versao) {
		return false;
	}

	public int insertVersoes(List<Versoes> versoes) {

		int totalInsert = 0;
		String sql = "INSERT versoes(NOME) VALUES(?)";
		PreparedStatement prest;
		try {
			for (Iterator<Versoes> it = versoes.iterator(); it.hasNext();) {
				Versoes versao = it.next();

				prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
				// prest.setString(1, null);
				prest.setString(1, versao.getNome());
				logger.debug("SQL insert statement " + prest.toString());
				int count = prest.executeUpdate();
				totalInsert += count;
			}
			logger.debug("Inseridas " + totalInsert + " no total");
		} catch (SQLException e1) {
			logger.debug("Cannot insert versoes on DB ", e1);
		}

		return 0;
	}

	public boolean updateVersao() {
		return false;
	}

	public boolean removeVersao() {
		return false;
	}

	public Connection getMysqlConn() {
		return mysqlConn;
	}

	public void setMysqlConn(Connection mysqlConn) {
		this.mysqlConn = mysqlConn;
	}
}